/* ----------------------------------
	     JAVA-JESS statements
---------------------------------- */ 
; always remember to import Java classes that are being used in this Jess file
(import dk.dtu.is.jjmvcp.model.MyJavaBeanModel)
(import dk.dtu.is.jjmvcp.addMVC)
; create the template for the javabean so that JESS con recognize it in the definition of the rules 
; and in other parts of this file. If this line was not declared, JESS would throw an error whenever
; it would encounter a "MyJavaBeanModel" reference, as it would not recognize it.
; The instance of this object will be pushed into JESS by Java through the function "addMVC"
(defclass MyJavaBeanModel MyJavaBeanModel)

/* ----------------------------------
			   GLOBAL
---------------------------------- */ 
; here you could add global variables for your JESS file
; For example the path of some local files you might want to read in JESS
; In this case, remember to use the double backslash as folder separator!
; An example is provided below:
;(defglobal ?*FILESPATH* = "C:\\Users\\dsac\\workspaces\\JavaJess\\bin")


/* ----------------------------------
			   MODULES
---------------------------------- */ 
; no modules in this prototype


/* ----------------------------------
			   TEMPLATE
---------------------------------- */ 

; this is simply a test fact template for MyJavaBeanModel values.
; We show how we can reason about the status of the java bean value. 
; the Status can be low, normal or high
(deftemplate BeanValueStatus (slot theStatus))


/* ----------------------------------
			  FUNCTION
---------------------------------- */
 
; used to have a console during the rules activation
; In this prototype to code must run-until-halt
; so there is no way to use this function. We leave it here 
; for completiness
(deffunction command-loop ()
    "Explicitly start a command loop from within the system"
    ((new jess.Jesp ((engine) getInputRouter "t") (engine)) parse TRUE))

; a very basic function to show how we can print a message
(deffunction FUNLIB::myprint (?message)
	(printout t ?message crlf)
)


/* ----------------------------------
				RULES
---------------------------------- */
; I have added some basic rules to show how to detect changes in the JavaBean
; (which works as a normal fact in JESS now) and how to assert new fact and
; change them based upon its value

(defrule detect-high-values
    "If the value is higher than 75, than it is a high value"
	(declare (no-loop TRUE))
    (MyJavaBeanModel (myValueOne ?value))
    (test (neq ?value nil)) ; avoid looking for an integer value as soon as the script is launched
    ?beanValueStatus <- (BeanValueStatus)
    (test (>= ?value 75))
	=>
;   (printout t ?value " is High Value." crlf)
   (modify ?beanValueStatus (theStatus "high"))
)

(defrule detect-normal-values
    "If the value is between 25 and 75, than it is a normal value"
    (declare (no-loop TRUE))
    (MyJavaBeanModel (myValueOne ?value))
    (test (neq ?value nil)) ; avoid looking for an integer value as soon as the script is lauched
    ?beanValueStatus <- (BeanValueStatus)
    (test (> ?value 25))
    (test (< ?value 75))
	=>
;    (printout t ?value " is Normal Value." crlf)
    (modify ?beanValueStatus (theStatus "normal"))
)

(defrule detect-low-values
    "If the value is lower than 25, than it is a low value"
    (declare (no-loop TRUE))
    (MyJavaBeanModel (myValueOne ?value))
    (test (neq ?value nil)) ; avoid looking for an integer value as soon as the script is lauched
    ?beanValueStatus <- (BeanValueStatus)
    (test (<= ?value 25))
	=>
;    (printout t ?value " is Low Value." crlf)
	(modify ?beanValueStatus (theStatus "low"))
)

/* ----------------------------------
				ACTIONS
---------------------------------- */
; Actions: this part of the program contains commands to be executed by the jess interpreter.

; add the MVC to the current JESS program
; the name of the function to be called here is defined by the method "public String getName()"
; which is defined in addMVC.java. In particular the name is defined by the private static final String functionName = "addMVC";
(load-function addMVC)
(addMVC)

; add some info on the console
(watch facts)

; init the rule bases system by having an invalid value
(assert (BeanValueStatus (theStatus "invalid")))

; start executing the agenda continously
(run-until-halt)